package com.majsubtense.core.components;

import javafx.animation.AnimationTimer;

public abstract class Timer extends Component {
    public static final ComponentType<Timer> ANY;
    protected long pastRunTime = 0;
    protected AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            if (pastRunTime == 0)
                pastRunTime = now;
            long deltaTime = now - pastRunTime;
            pastRunTime = now;

            update(deltaTime);
        }
    };

    public Timer() {
        super(Timer.ANY);
    }

    public void timerStart() {
        timer.start();
    }

    public void timerStop() {
        timer.stop();
    }

    public void timerWait(long timeoutMillis, int nanos) throws InterruptedException {
        timer.wait(timeoutMillis, nanos);
    }

    public abstract void update(long deltaTime);

    static {
        ANY = new ComponentType<>(ComponentType.ROOT, "TIMER");
    }
}
